<template>
  <div class="v-editor-models-tree-select g-jc-sb">
    <!-- {{row.value}} -->
    <div
      v-if="mode === 'horizontal'"
      class="horizontal"
    >
      <div
        v-for="(item, index) of valueData"
        :key="index"
        class="value-item"
        @click="handleClickItem(item)"
      >
        <img
          style="margin-right: 4px; height: 16px"
          :src="item.icon"
        />
        <span v-if="item.name">{{ item.name }}</span>
      </div>

      <div
        v-if="selected && !row.readonly && !specialModelDisabled && !isModelDisabled && !tableElementDisabled"
        class="add-btn"
      >
        <img
          src="statics/icons/app/iconmore.png"
          class="g-pointer"
          @click="handleAdd"
        />
      </div>
    </div>
    <PropertyNodeLabel
      v-else
      class="_list_scroll"
      :row="row"
      :encrypted="encrypted"
      :selected="selected"
      :selectedModelId="selectedModelId"
      @clickItem="handleClickItem"
    ></PropertyNodeLabel>

    <div
      v-if="selected && !row.readonly && !specialModelDisabled && !isModelDisabled && mode !== 'horizontal'"
      style="border-left: 1px solid #ddd; line-height: 14px"
      class="g-pd-4 g-flex-0"
    >
      <div>
        <img
          src="statics/icons/app/iconadd.png"
          class="g-pointer"
          @click="handleAdd"
        />
      </div>
      <div class="g-m-t-4">
        <img
          src="statics/icons/app/iconreduce.png"
          class="g-pointer"
          :class="{ _disable: disabledRemove }"
          @click="handleRemove"
        />
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { actionManager } from "@/action/ActionManager";
import { PropertyRow, ModelValue } from "../../types";
import { ref, Ref, computed, watch, inject } from "vue";
import { PropertyKey } from "@/constants/Enums";
import { propertyUtil } from "../../PropertyUtil";
import PropertyNodeLabel from "../custom/PropertyNodeLabel.vue";
import { ActionKey } from "@/action/ActionKey";
import { OpenCreateElementAction } from "@/modules/model/actions/model/OpenCreateElementAction";
import { modelService } from "@/modules/model/service";
import { GraphNode } from "@/modules/model/project/GraphNode";
import { NodeData } from "@/modules/model/project/IGraphNode";
import { Project } from "@/modules/model/project/Project";

const isModelDisabled = inject("isModelDisabled", ref(false)) as Ref<boolean>;

/**
 * 模型多选-树选择弹窗
 */
const props = defineProps<{
  row: PropertyRow;
  selected?: boolean;
  tableElementDisabled?: boolean;
  encrypted?: boolean;
  mode?: string; // 新增横向horizontal模式, 以便在一行内显示内容
}>();

const valueData = computed(() => {
  return (props.row.value || []) as ModelValue[];
});

const specialModelDisabled = computed(() => {
  const specalPropertyKey = new Set([PropertyKey["Element_ownedElement"]]);
  return specalPropertyKey.has(props.row.key as PropertyKey);
});

const selectedModelId = ref("");

const disabledRemove = computed(() => {
  return valueData.value.length == 0 || (props.row.nullable === false && valueData.value.length == 1);
});

watch(
  () => props.selected,
  val => {
    if (val === false) {
      selectedModelId.value = "";
    }
  }
);

const handleAdd = async () => {
  const projectId = app.activeProject?.id;
  if (!projectId) return;
  const row = props.row;
  let node = app.activeProject.nodePool.getNode(row.modelId);
  if (!node) {
    const nodeData = (await modelService.getModelData(projectId, row.modelId)) as NodeData;
    node = new GraphNode(nodeData, app.activeProject as Project);
  }

  const action = actionManager.getAction(ActionKey.openCreateElement) as OpenCreateElementAction;
  action.onTreeMenuClick(node);
};

function handleRemove() {
  if (disabledRemove.value) return;
  if (!selectedModelId.value) {
    app.$notify({
      title: "提示",
      type: "error",
      message: "请选中元素再操作移除"
    });
    return;
  }
  propertyUtil.deletePropertyAndRefresh(props.row, selectedModelId.value);
}

function handleClickItem(item: { id: string }) {
  selectedModelId.value = item.id;
}
</script>
<style lang="scss" scoped>
@import "@/assets/css/theme.scss";
.v-editor-models-tree-select {
  // height: 100%;
  ._disable {
    cursor: not-allowed;
    color: #c1c5cb;
    filter: grayscale(100);
  }
  ._selected {
    color: $theme-color;
  }
  ._list_scroll {
    overflow-y: auto;
    &::-webkit-scrollbar {
      width: 8px;
      height: 8px;
    }
  }
  .horizontal {
    height: 24px;
    width: 100%;
    padding-right: 30px;
    padding-left: 11px;
    position: relative;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    line-height: 24px;
    .value-item {
      display: inline-block;
      height: 16px;
      margin-right: 10px;
      line-height: 16px;
      img {
        margin-right: 6px;
        line-height: 16px;
        vertical-align: middle;
      }
      span {
        line-height: 14px;
        vertical-align: middle;
      }
    }
    .add-btn {
      line-height: 1;
      position: absolute;
      top: 50%;
      right: 5px;
      transform: translateY(-50%);
    }
  }
}
</style>
